﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MusicOnLine.Models;
using MusicOnLine.Common;
namespace MusicOnLine.Controllers
{
    public class HomeController : Controller
    {
        MusicPlayEntities musicDB = new MusicPlayEntities();
        public ActionResult Index()
        {
            //var userList = musicDB.Memberships.Where(o =>new string[] { "victor","sharp"}.ToList().IndexOf(o.UserName) > 0).Select(o => o.UserID);
                
            return View();
        }       

        public ActionResult TopSongs(string orderFlag)
        {
            IEnumerable<AACInfo> model;
            if (orderFlag == "playcount")
            {
                model = musicDB.AACInfoes.OrderByDescending(o => o.PlayedCount).Take(50).ToList();
            }
            else
            {
                model = musicDB.AACInfoes.OrderByDescending(o => o.CreateDate).Take(50).OrderByDescending(o=>o.PlayedCount).ToList();
            }
           
            return PartialView(model);
        }

        public ActionResult AlbumSongs(int ID)
        {
            AlbumInfo albumInfo = new AlbumInfo();
            albumInfo.Album = musicDB.Albums.Single(obj => obj.ID == ID);
            albumInfo.SongsList = musicDB.AACInfoes.Where(obj => obj.AlbumID == ID).ToList();
            return View(albumInfo);
        }

        public ActionResult SingerList()
        {
            var model = from u in musicDB.Artists
                        orderby u.StartWith ascending, u.ArtistName ascending
                        select u;
            return PartialView(model);
        }

        public ActionResult SearchResult()
        {
            return View();
        }

        public ActionResult SearchSongs(string g, int PageIndex)
        {
            PageList<AACInfo> mode = new PageList<AACInfo>(musicDB.AACInfoes.Where(o => o.Name.Contains(g) || o.Album.Contains(g) || o.Artist.Contains(g)).OrderByDescending(o=>o.CreateDate), PageIndex, 20);
            return Json(mode, JsonRequestBehavior.AllowGet);
        }

        public ActionResult QuickSearch(string term)
        {
            return Json(musicDB.AACInfoes.Where(o => o.Name.Contains(term)).Select(o => new { value = o.Name }).Take(10).ToList(),JsonRequestBehavior.AllowGet);
        }

        public ActionResult NewTopAlbums()
        {
            var mode = musicDB.Albums.OrderByDescending(o => o.CreateDate).Take(9).ToList();
            return View(mode);
        }


        public ActionResult TopAlbums()
        {
            var mode = (from a in musicDB.AACInfoes
                        join b in musicDB.Albums on new { AlbumID = (Int32)a.AlbumID } equals new { AlbumID = b.ID }
                        group a by new
                        {
                            a.AlbumID,
                            b.ImgUrl,
                            b.AlbumName,
                            b.ArtistName,
                            b.ArtistID
                        } into g
                        orderby
                         g.Sum(p => p.PlayedCount) descending
                        select new 
                        {                             
                            ID = (int)g.Key.AlbumID,
                            ImgUrl = g.Key.ImgUrl,
                            AlbumName = g.Key.AlbumName,
                            ArtistName = g.Key.ArtistName,
                            ArtistID=g.Key.ArtistID
                        }).Take(27).ToList();
           return View(mode);
        }

        public ActionResult ArtistsList() 
        {
            var model = from u in musicDB.Artists
                        orderby u.StartWith ascending, u.ArtistName ascending
                        select u;
            return View(model);
        }

        protected override void Dispose(bool disposing)
        {
            musicDB.Dispose();
            base.Dispose(disposing);
        }
    }
}
